﻿using System.Web.Mvc;
using WebMatrix.WebData;
using System.Linq;
using System.Windows;
using System;
//using EasyProjectDB.Models;
using MvcEasyProject.Models;
using MvcEasyProject.ViewModels;
using DbProject;
using DbProject.Models;
using System.Web.Security;

namespace MvcEasyProject.Controllers
{
    
    
    public class HomeController : Controller
    {
        public DateTime ThisTime
        {
            get { return DateTime.Now; }
        }
       // public string setCheck { set; get; } 
        public ActionResult Index()
        {
            HomePageModel model = new HomePageModel();
            var today = DateTime.Now;
            using (var db = new DbProjectContext())
           
            
            {
                var TimeStamp = new WorkerTimeStampModel();
                if (db.Workers.Count() == 0)
                {
                    var AdminWorker = new WorkerModel();

                    AdminWorker.FirstName = "משה";
                    AdminWorker.LastName = "אזגרי";
                    AdminWorker.JobId = 1;
                    AdminWorker.UserName = "moshe";
                    AdminWorker.PhoneNumber = "0504577748";
                    AdminWorker.WorkerId = 1;
                    AdminWorker.GovermentIdNumber = "040328254";
                    //Roles.AddUserToRole("SysAdministrator", "Administrator");
                    //בדיקה
                    db.Workers.Add(AdminWorker);
                    db.SaveChanges();
                }
                var CurrentWorker = db.Workers.FirstOrDefault(w => w.UserName == Environment.UserName);
                if (CurrentWorker == null) 
                {
                    return RedirectToAction("Login", "Account");
                }
                    var workerTimeStamps= db.TimeStamps.Where(t => t.WorkerId == CurrentWorker.WorkerId);
                    TimeStamp = workerTimeStamps.FirstOrDefault(d => d.LogIn == workerTimeStamps.Max(s => s.LogIn) );

                if (TimeStamp == null || TimeStamp.LogIn.Date < today.Date )
                {
                    TimeStamp = new WorkerTimeStampModel(); 
                    TimeStamp.WorkerId = CurrentWorker.WorkerId;
                    TimeStamp.LogIn = today;
                    TimeStamp.LogOut = today;
                    //TimeStamp.Id = db.TimeStamps.Count() + 1;
                    db.TimeStamps.Add(TimeStamp);
                    db.SaveChanges();
                }
               
                if (CurrentWorker == null)
                {

                    ModelState.AddModelError("", "This worker is not registerd please conntact SYS administrator");
                    return View();
                }
                model.Worker = CurrentWorker;

                model.TimeStamp = TimeStamp;
                model.stampCollaction = db.TimeStamps.Where(t => t.WorkerId == CurrentWorker.WorkerId).ToList();
                //TODO: add to model login formt model and if the worker hasent sign in add mbox for sign in

                return View(model);
            }



        }

    }
}
